/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bash.jcouture.util.swing;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JTable;
import javax.swing.table.TableModel;

/**
 *
 * @author bash
 */
public class ExcelExporter {


    public ExcelExporter() {
    }

    public static void exportTable(JTable table, File file,boolean append) throws IOException {
        TableModel model = table.getModel();
        try {
            FileWriter out = new FileWriter(file,append);

            for (int i = 0; i < model.getColumnCount(); i++) {
                out.write(model.getColumnName(i) + "\t");
            }
            out.write("\n");

            for (int i = 0; i < model.getRowCount(); i++) {
                for (int j = 0; j < model.getColumnCount(); j++) {

                    if (j == 0) {

                        out.write(model.getValueAt(i, j).toString().replace(".",",") + "\t");
                    } else {
                       
                        out.write(model.getValueAt(i, j).toString().replace(".",",") + "\t");
                    }
                }
                out.write("\n");
            }
            out.close();
            System.out.println("write to " + file.getAbsolutePath());
        } catch (NullPointerException n) {
            return;
        }
    }

    public static void exportTables(File f,JTable... tables) throws IOException{


        for(JTable t:tables){
            exportTable(t, f, true);
        }
    }
}